package ar.com.rpsoftware.groupsphase;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import ar.com.rpsoftware.classes.MatchesFragment;
import ar.com.rpsoftware.fixturebrasil2014.R;

public class GroupsPhaseActivity extends FragmentActivity {

	private TabHost UItabContainer;
	private ViewPager UIgroupsContentPager;
	private GroupContentPagerAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.groups_layout);
		
		// Obtengo elementos UI
		UIgroupsContentPager = (ViewPager) findViewById(R.id.groupsContentPager);
		UItabContainer = (TabHost) findViewById(android.R.id.tabhost);	
		
		// Configuro los tabs
		setTabs();		
		
		adapter = new GroupContentPagerAdapter(getSupportFragmentManager());
		
		UIgroupsContentPager.setAdapter(adapter);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	private void setTabs() {
			
		UItabContainer.setup();
		
		// Creo el factory que me crea cada contenido de tab
		TabHost.TabContentFactory factory = new TabHost.TabContentFactory() {
			
			@Override
			public View createTabContent(String tag) {							
				
				ViewPager pager = (ViewPager) findViewById(R.id.groupsContentPager);
				
				return pager;
				
			}
		};
		
		// Creo los tabs para los grupos
		// Grupo A
		TabSpec tab1 = UItabContainer.newTabSpec(getResources().getString(R.string.grupoA));
		tab1.setIndicator(getResources().getString(R.string.grupoA));
		tab1.setContent(factory);
		// Grupo B
		TabSpec tab2 = UItabContainer.newTabSpec(getResources().getString(R.string.grupoB));
		tab2.setIndicator(getResources().getString(R.string.grupoB));
		tab2.setContent(factory);
		// Grupo C
		TabSpec tab3 = UItabContainer.newTabSpec(getResources().getString(R.string.grupoC));
		tab3.setIndicator(getResources().getString(R.string.grupoC));
		tab3.setContent(factory);
		// Grupo D
		TabSpec tab4 = UItabContainer.newTabSpec(getResources().getString(R.string.grupoD));
		tab4.setIndicator(getResources().getString(R.string.grupoD));
		tab4.setContent(factory);
		// Grupo E
		TabSpec tab5 = UItabContainer.newTabSpec(getResources().getString(R.string.grupoE));
		tab5.setIndicator(getResources().getString(R.string.grupoE));
		tab5.setContent(factory);
		// Grupo F
		TabSpec tab6 = UItabContainer.newTabSpec(getResources().getString(R.string.grupoF));
		tab6.setIndicator(getResources().getString(R.string.grupoF));
		tab6.setContent(factory);
		// Grupo G
		TabSpec tab7 = UItabContainer.newTabSpec(getResources().getString(R.string.grupoG));
		tab7.setIndicator(getResources().getString(R.string.grupoG));
		tab7.setContent(factory);
		// Grupo H
		TabSpec tab8 = UItabContainer.newTabSpec(getResources().getString(R.string.grupoH));
		tab8.setIndicator(getResources().getString(R.string.grupoH));
		tab8.setContent(factory);
		
		// Agrego los tabs
		UItabContainer.addTab(tab1);
		UItabContainer.addTab(tab2);
		UItabContainer.addTab(tab3);
		UItabContainer.addTab(tab4);
		UItabContainer.addTab(tab5);
		UItabContainer.addTab(tab6);
		UItabContainer.addTab(tab7);
		UItabContainer.addTab(tab8);

		// Acomodo los tabs
		for (int i = 0; i < UItabContainer.getTabWidget().getChildCount(); i++) 
	    { 
			// Padding - espaciado
			UItabContainer.getTabWidget().getChildAt(i).setPadding(40,0,40,0);
			// Estilo
			// UItabContainer.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_style);
	    }
		
	}
	
	private class GroupContentPagerAdapter extends FragmentStatePagerAdapter {
		
		private final static int NUM_PAGES = 2;
		
        public GroupContentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
        	
        	// switch de acuerdoo al num de pagina, devuelvo fragment partidos o fragment tabla de posiciones
        	
        	MatchesFragment fg = new MatchesFragment();
        	Bundle data = new Bundle();
        	
        	data.putInt("number", position);
        	
        	fg.setArguments(data);
        	
            return fg;
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
	
}
